package com.platform.config;

import com.platform.core.consts.CommonConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

@Slf4j
public class FeignConfig{
    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (null != requestAttributes) {
                    HttpServletRequest request = requestAttributes.getRequest();// 老请求
                    if (request != null) {
                        String authorization = request.getHeader(CommonConstant.TOKEN_NAME);
                        requestTemplate.header(CommonConstant.TOKEN_NAME, authorization);
                    }
                }

            }
        };
    }
}
